package com.ruoyi.web.controller.gcgl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.ReturnUUID;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.SjProjectNsct;
import com.ruoyi.gcgl.domain.SjProjectNsctitem;
import com.ruoyi.gcgl.domain.SjProjectPeople;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.ISjProjectNsctService;
import com.ruoyi.gcgl.service.ISjProjectNsctitemService;
import com.ruoyi.gcgl.service.ISjProjectPeopleService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 项目内审出图Controller
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Controller
@RequestMapping("/sj/nsct")
public class SjProjectNsctController extends BaseController
{
    private String prefix = "sj/nsct";

    @Autowired
    private ISjProjectNsctService sjProjectNsctService;
    @Autowired
    private ISjProjectNsctitemService sjProjectNsctitemService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISjProjectPeopleService sjProjectPeopleService;
    @Autowired
    private ISysRoleService roleService;

    @RequiresPermissions("gcgl:nsct:view")
    @GetMapping()
    public String nsct()
    {
        return prefix + "/nsct";
    }

    /**
     * 查询项目内审出图列表
     */
    @RequiresPermissions("gcgl:nsct:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SjProjectNsct sjProjectNsct)
    {
        startPage();
        sjProjectNsct.setDeleteFlag(1);
      //角色数据权限 ==根据用户定义的数据权限查看项目数据
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
        int count = roleService.selectCountRoleDeptByRoleId(roleIds);
        if(count != 0){
        	//看部门
        	sjProjectNsct.setRoleIds(roleIds);
        }else{
        	//看个人
        	if(getSysUser().isShowRoleIds(roleIds)){
        		sjProjectNsct.setOwner(getLoginName());
            }
        }
        //----
        List<SjProjectNsct> list = sjProjectNsctService.selectSjProjectNsctList(sjProjectNsct);
        return getDataTable(list);
    }

    /**
     * 导出项目内审出图列表
     */
    @RequiresPermissions("gcgl:nsct:export")
    @Log(title = "项目内审出图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SjProjectNsct sjProjectNsct)
    {
        List<SjProjectNsct> list = sjProjectNsctService.selectSjProjectNsctList(sjProjectNsct);
        ExcelUtil<SjProjectNsct> util = new ExcelUtil<SjProjectNsct>(SjProjectNsct.class);
        return util.exportExcel(list, "项目内审出图数据");
    }

    /**
     * 新增项目内审出图
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap){
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	mmap.put("orderNo", ReturnUUID.getOrderNo("HT"));
    	
    	SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList(new SysUser()));
        
        mmap.put("owner", getLoginName());
    	mmap.put("ownerId", getUserId());
    	mmap.put("deptName", getSysUser().getDept().getDeptName());
    	mmap.put("deptId", getSysUser().getDept().getDeptId());
        
        return prefix + "/add";
    }

    /**
     * 新增保存项目内审出图
     */
    @RequiresPermissions("gcgl:nsct:add")
    @Log(title = "项目内审出图", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SjProjectNsct sjProjectNsct){
    	SysUser user = userService.selectUserById(sjProjectNsct.getSprId());
    	if(user!=null){
    		sjProjectNsct.setSpr(user.getUserName());
    	}
    	
    	SysUser user1 = userService.selectUserById(sjProjectNsct.getJdrId());
    	if(user1!=null){
    		sjProjectNsct.setJdr(user1.getUserName());
    	}
    	
    	sjProjectNsct.setStatus(1);
    	sjProjectNsct.setDeleteFlag(1);
    	sjProjectNsct.setCreateUser(getLoginName());
    	sjProjectNsct.setCreateAt(new Date());
    	sjProjectNsct.setUpdateUser(getLoginName());
    	sjProjectNsct.setUpdateAt(new Date());
        return toAjax(sjProjectNsctService.insertSjProjectNsct(sjProjectNsct));
    }

    /**
     * 修改项目内审出图
     */
    @RequiresPermissions("gcgl:nsct:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap){
        SjProjectNsct sjProjectNsct = sjProjectNsctService.selectSjProjectNsctById(id);
        mmap.put("sjProjectNsct", sjProjectNsct);
        
        mmap.put("zilwjvo", sjProjectNsct.getZilwj());
        if(sjProjectNsct.getZilwj()!=null && !sjProjectNsct.getZilwj().equals("")){
        	String[] zlis = sjProjectNsct.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	sjProjectNsct.setZilwj(file);
        }
        
        mmap.put("zilwjzsvo", sjProjectNsct.getZilwjzs());
        if(sjProjectNsct.getZilwjzs()!=null && !sjProjectNsct.getZilwjzs().equals("")){
        	String[] zlis = sjProjectNsct.getZilwjzs().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	sjProjectNsct.setZilwjzs(file);
        }
        
        SysUser user = getSysUser();
        mmap.put("user", user);
        mmap.put("userlist", userService.selectUserList(new SysUser()));
        return prefix + "/edit";
    }

    /**
     * 修改保存项目内审出图
     */
    @RequiresPermissions("gcgl:nsct:edit")
    @Log(title = "项目内审出图", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SjProjectNsct sjProjectNsct){
    	SysUser user = userService.selectUserById(sjProjectNsct.getSprId());
    	if(user!=null){
    		sjProjectNsct.setSpr(user.getUserName());
    	}
    	
    	SysUser user1 = userService.selectUserById(sjProjectNsct.getJdrId());
    	if(user1!=null){
    		sjProjectNsct.setJdr(user1.getUserName());
    	}
    	
    	sjProjectNsct.setUpdateUser(getLoginName());
    	sjProjectNsct.setUpdateAt(new Date());
        return toAjax(sjProjectNsctService.updateSjProjectNsct(sjProjectNsct));
    }
    
    @RequiresPermissions("gcgl:nsct:edit")
    @Log(title = "项目内审出图最终", businessType = BusinessType.UPDATE)
    @PostMapping("/editZz")
    @ResponseBody
    public AjaxResult editZzSave(SjProjectNsct sjProjectNsct){
    	SysUser user = userService.selectUserById(sjProjectNsct.getSprId());
    	if(user!=null){
    		sjProjectNsct.setSpr(user.getUserName());
    	}
    	
    	SysUser user1 = userService.selectUserById(sjProjectNsct.getJdrId());
    	if(user1!=null){
    		sjProjectNsct.setJdr(user1.getUserName());
    	}
    	
    	sjProjectNsct.setStatus(6);
    	sjProjectNsct.setUpdateUser(getLoginName());
    	sjProjectNsct.setUpdateAt(new Date());
        return toAjax(sjProjectNsctService.updateSjProjectNsct(sjProjectNsct));
    }

    /**
     * 删除项目内审出图
     */
    @RequiresPermissions("gcgl:nsct:remove")
    @Log(title = "项目内审出图", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sjProjectNsctService.deleteSjProjectNsctByIds(ids));
    }
    
    @GetMapping("/p4nsct/{id}")
    public String p4nsct(ModelMap mmap,@PathVariable("id") Long id){
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	//判断是成员，还是管理员
    	List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
    	//看个人
        int flag = 0;//成员
    	if(getSysUser().isShowRoleIds(roleIds)){
    		SjProjectPeople sjProjectPeople = new SjProjectPeople();
            sjProjectPeople.setDeleteFlag(1);
            sjProjectPeople.setGcProjectid(id);
            sjProjectPeople.setCode(getUserId().toString());
            List<SjProjectPeople> peoplelis = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
            if(peoplelis!=null && peoplelis.size()>0){
            	for(SjProjectPeople po1:peoplelis){
                	if(po1.getpId()==0l){
                		flag = 1;//领导
                		break;
                	}
                }
            }
        }else{
        	flag = 1;//领导
        }
        mmap.put("flag", flag);
        return "sj/project/projectDetailNsct";
    }

    /**
     * 查询项目内审出图列表
     */
    @PostMapping("/p4list")
    @ResponseBody
    public TableDataInfo p4list(SjProjectNsct sjProjectNsct){
        startPage();
        sjProjectNsct.setDeleteFlag(1);
        List<SjProjectNsct> list = sjProjectNsctService.selectSjProjectNsctList(sjProjectNsct);
        
        for(SjProjectNsct ms:list){
        	GcProject po = gcProjectService.selectGcProjectById(ms.getGcProjectid());
        	ms.setProjectName(po.getName());
        }
        return getDataTable(list);
    }
    
    ///-------------------Item
    @PostMapping("/p4listItem")
    @ResponseBody
    public TableDataInfo p4listItem(SjProjectNsctitem sjProjectNsctitem){
        startPage();
        sjProjectNsctitem.setDeleteFlag(1);
        List<SjProjectNsctitem> list = sjProjectNsctitemService.selectSjProjectNsctitemList(sjProjectNsctitem);
        return getDataTable(list);
    }
    
    /**
     * 新增项目内审出图
     */
    @GetMapping("/addItem/{id}")
    public String addItem(@PathVariable("id") Long id, ModelMap mmap){
    	
    	SjProjectNsct sjProjectNsct = sjProjectNsctService.selectSjProjectNsctById(id);
    	mmap.put("sjProjectNsct", sjProjectNsct);
    	
    	GcProject po = gcProjectService.selectGcProjectById(sjProjectNsct.getGcProjectid());
    	mmap.put("gcProject", po);
    	
        return prefix + "/addItem";
    }

    /**
     * 新增保存项目内审出图
     */
    @Log(title = "项目内审出图", businessType = BusinessType.INSERT)
    @PostMapping("/addItem")
    @ResponseBody
    public AjaxResult addItemSave(SjProjectNsctitem sjProjectNsctitem){
    	sjProjectNsctitem.setAnswerPr(getLoginName());
    	sjProjectNsctitem.setAnswerAt(new Date());
    	sjProjectNsctitem.setDeleteFlag(1);
    	sjProjectNsctitem.setCreateUser(getLoginName());
    	sjProjectNsctitem.setCreateAt(new Date());
    	sjProjectNsctitem.setUpdateUser(getLoginName());
    	sjProjectNsctitem.setUpdateAt(new Date());
        return toAjax(sjProjectNsctitemService.insertSjProjectNsctitem(sjProjectNsctitem));
    }

    /**
     * 修改项目内审出图
     */
    @GetMapping("/editItem/{id}")
    public String editItem(@PathVariable("id") Long id, ModelMap mmap){
        SjProjectNsct sjProjectNsct = sjProjectNsctService.selectSjProjectNsctById(id);
        mmap.put("sjProjectNsct", sjProjectNsct);
        
        mmap.put("zilwjvo", sjProjectNsct.getZilwj());
        if(sjProjectNsct.getZilwj()!=null && !sjProjectNsct.getZilwj().equals("")){
        	String[] zlis = sjProjectNsct.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	sjProjectNsct.setZilwj(file);
        }
        
        mmap.put("zilwjzsvo", sjProjectNsct.getZilwjzs());
        if(sjProjectNsct.getZilwjzs()!=null && !sjProjectNsct.getZilwjzs().equals("")){
        	String[] zlis = sjProjectNsct.getZilwjzs().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	sjProjectNsct.setZilwjzs(file);
        }
        return prefix + "/editItem";
    }

    /**
     * 修改保存项目内审出图
     */
    @Log(title = "项目内审出图", businessType = BusinessType.UPDATE)
    @PostMapping("/editItem")
    @ResponseBody
    public AjaxResult editItemSave(SjProjectNsct sjProjectNsct){
    	sjProjectNsct.setUpdateUser(getLoginName());
    	sjProjectNsct.setUpdateAt(new Date());
        return toAjax(sjProjectNsctService.updateSjProjectNsct(sjProjectNsct));
    }
    
    @PostMapping("/zhct")//最后出图
    @ResponseBody
    public AjaxResult proClose(SjProjectNsct sjProjectNsct){
    	sjProjectNsct.setStatus(3);
    	sjProjectNsctService.updateSjProjectNsct(sjProjectNsct);
        return toAjax(1);
    }
}